import utils from "@utils";

/*
 * @Author: xiaosihan 
 * @Date: 2023-10-11 23:42:43 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2023-11-18 06:29:24
 */

import queryString from 'query-string';

class WxService {

    constructor() {

    }

    async loadWxLoginJS() {
        if (!window.WxLogin) {
            await new Promise<void>((resolve, reject) => {
                const script = document.createElement("script");
                script.src = "//res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js";
                document.head.appendChild(script);
                script.onload = () => {
                    resolve();
                }
            });
        }
    }

    getQueryString = (key: string) => {
        const parsedHash = queryString.parse(location.search);
        return parsedHash[key];
    }

    // 调用微信登录
    async login(domId: string) {
        await this.loadWxLoginJS();

        //通过状态跳转当前环境的回调地址
        let state = this.getQueryString("state");

        var obj = new window.WxLogin({
            self_redirect: true,
            id: domId,
            appid: "wx871e0bb5757a8a4c",
            scope: "snsapi_login",
            state,
            redirect_uri: window.decodeURI(`${location.protocol}//${location.host}`),
            href: 'data:text/css;base64,LnBhbmVsQ29udGVudCAuaW1wb3dlckJveCAucXJjb2RlIHt3aWR0aDogMTYwcHg7fQoucGFuZWxDb250ZW50IC5wYW5lbENvbnRlbnQgLmluZm8ge2Rpc3BsYXk6IG5vbmU7fQoucGFuZWxDb250ZW50IC50aXRsZSB7ZGlzcGxheTogbm9uZTt9'
        });

        console.log(obj, 'xxx');

    }

    //
    //微信支付
    async pay() {

    }

    // Content-Type: application/json
    // Accept: application/json

}

const wxService = new WxService();

export default wxService;